package com.itheima.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
     * request通用方式获取请求参数，使doGet方法和doPost方法代码统一
 */

@WebServlet("/RequestDemo5")
public class RequestDemo5 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //tomcat会将所有的请求参数解析后存取map集合中
        Map<String, String[]> parameterMap = request.getParameterMap();
        //遍历map集合，拿到参数值
        for (String key : parameterMap.keySet()) {
            System.out.print(key+":");
            //根据key获取所有的参数值,value是字符数组
            String[] parameterValues = request.getParameterValues(key);
            for (String parameterValue : parameterValues) {
                System.out.print(parameterValue+" ");
            }
            System.out.println();
        }

        //根据参数名获取某个参数（单个）
        String username = request.getParameter("username");
        System.out.println(username);
        String password = request.getParameter("password");
        System.out.println(password);

        //根据参数名获取多个值
        String[] hobbys = request.getParameterValues("hobbys");
        for (String hobby : hobbys) {
            System.out.println(hobby);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}